﻿#include "idamainwindow.h"
#include "ui_IdaMainWindow.h"
#include <QTimer>
#include <QFile>
#include <QScreen>
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include "SAFramelessHelper.h"
#include "BaseWidget/SAFramelessHelper.h"
#include "BaseModule/IBaseSignal.h"
#include "IdaTitleBar.h"


class SARibbonMainWindowPrivate
{
public:
    SARibbonMainWindowPrivate(IdaMainWindow* p);
    void init();

    IdaMainWindow* Parent;
    //SARibbonBar* ribbonBar;
    //SARibbonMainWindow::RibbonTheme currentRibbonTheme;
    //SAWindowButtonGroup* windowButtonGroup;
    SAFramelessHelper* pFramelessHelper;
    bool useRibbon;
};

SARibbonMainWindowPrivate::SARibbonMainWindowPrivate(IdaMainWindow* p)
    : Parent(p)
    //, ribbonBar(nullptr)
    //, currentRibbonTheme(IdaMainWindow::Office2013)
    //, windowButtonGroup(nullptr)
    , pFramelessHelper(nullptr)
    , useRibbon(true)
{
}

void SARibbonMainWindowPrivate::init()
{
}

IdaMainWindow::IdaMainWindow(QWidget* parent, bool m_isDefaultUI)
	: QMainWindow(parent)
    , m_titlebar(new IdaTitleBar(this))
    , m_isDefaultUI(m_isDefaultUI)
    , m_d(new SARibbonMainWindowPrivate(this))
{
    if (m_isDefaultUI)
    {
        ui = new Ui::IdaMainWindow();
        ui->setupUi(this);
    }
    m_d->pFramelessHelper = new SAFramelessHelper(this);
	initWindow();
	QTimer::singleShot(0, this, &IdaMainWindow::initData);
}

IdaMainWindow::~IdaMainWindow()
{
    if (m_isDefaultUI) delete ui;

    if (m_titlebar)
    {
        delete m_titlebar;
        m_titlebar = nullptr;
    }

}

/// @brief 初始化窗体

void IdaMainWindow::initWindow()
{
    this->setContextMenuPolicy(Qt::NoContextMenu);
    m_titlebar->setHeight(32);

    if (m_isDefaultUI)
    {
        //隐藏菜单栏
        ui->menuBar->hide();
        //隐藏状态栏
        ui->statusBar->hide();
        //设置工具栏
        ui->mainToolBar->hide();
        ui->mainToolBar->setMovable(false);
        ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon);       
    }


    connect(m_titlebar, &IdaTitleBar::signal_windowStateChange, this, &IdaMainWindow::signal_windowStateChange);
    connect(m_titlebar, &IdaTitleBar::signal_closeButtonClick, this, &IdaMainWindow::signal_closeButtonClick);
    connect(IBaseSignalInstance, &BaseModule::IBaseSignal::signal_updateWidgetCursor, this, &IdaMainWindow::slot_updateWidgetCursor);
    connect(IBaseSignalInstance, &BaseModule::IBaseSignal::signal_resetWidgetCursor, this, &IdaMainWindow::slot_resetWidgetCursor);
}

/// @brief 初始化数据

void IdaMainWindow::initData()
{
    moveToCenter();

}

void IdaMainWindow::setInitializationWindowMaximization()
{
    m_titlebar->setInitializationWindowMaximization();
}

void IdaMainWindow::setIcon(const QIcon& icon)
{
    m_titlebar->setIcon(icon);
}

void IdaMainWindow::slot_updateWidgetCursor(Qt::CursorShape shap)
{
    this->setCursor(shap);
}

void IdaMainWindow::slot_resetWidgetCursor()
{
    this->unsetCursor();
}

void IdaMainWindow::moveToCenter()
{
    if (isMaximized() || isFullScreen()) return;

    auto _screen = qApp->screenAt(this->geometry().center());
    if (_screen)
    {
        auto _windowRect = this->geometry();
        _windowRect.moveCenter(_screen->geometry().center());
        this->move(_windowRect.topLeft());
    }
}

SAFramelessHelper* IdaMainWindow::framelessHelper()
{
    return (m_d->pFramelessHelper);
}


void IdaMainWindow::setWindowButtonType(BaseWidget::ETitleBars type)
{
    m_titlebar->setWindowButtonType(type);
}

void IdaMainWindow::setTitleBarHeight(int height)
{
    m_titlebar->setHeight(height);
}

void IdaMainWindow::setTitleFontSize(int size)
{
    m_titlebar->setTitleFontSize(size);
}

void IdaMainWindow::setIsMainWindow(bool isMainWindow)
{
    m_titlebar->setIsMainWindow(isMainWindow);
}
